package com.chuanyang.driverh.base;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.List;


public abstract class BaseListFragment<T> extends BaseFragment implements
        OnItemClickListener, SpringView.OnFreshListener {
    public ListView mlistview;
    public View mEmptyView;
    TextView mEmptyText;
    ImageView mEmptyImage;
    public LinearLayout root;

    /**
     * 数据源
     */
    protected BaseListAdapter<T> mAdapter;
    protected int mCurrentPage = 1;
//    protected boolean isVisible;
    protected SpringView springView;
    @Override
    protected int getLayoutResId() {
        return R.layout.base_listview;
    }


    /**
     * 添加头部
     *
     * @return
     */
    protected View initHeaderView() {
        return null;
    }

    @Override
    public void initView(View view) {
        //找到baselist的根布局
        root = (LinearLayout) rootView.findViewById(R.id.base_listfrag_root);
        //找到基类中需要用到的控件
        springView = (SpringView) rootView.findViewById(R.id.springview);
        mlistview = (ListView) rootView.findViewById(R.id.lv_list);
        mEmptyView = rootView.findViewById(R.id.pull_refresh_empty_layout);
        mEmptyText = (TextView) rootView.findViewById(R.id.pull_refresh_empty_text);
        mEmptyImage = (ImageView) rootView.findViewById(R.id.pull_refresh_empty_image);

        if(initHeaderView()!=null&&mlistview.getHeaderViewsCount() == 0)
        {
            mlistview.addHeaderView(initHeaderView());
        }
        mAdapter = createAdapter();
        springView.setListener(this);
        mlistview.setAdapter(mAdapter);
        mlistview.setOnItemClickListener(this);
        springView.setType(SpringView.Type.FOLLOW);
        if (isPullToRefresh()) {
            springView.setHeader(new DefaultHeader(getActivity()));
        }
        if (isLoadMore()) {
            springView.setFooter(new DefaultFooter(getActivity()));
        }

    }



    /**
     * 数据为空
     *
     * @return
     */
    protected View getEmptyView() {
        return mEmptyView;
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 覆写
    }

    /**
     * 设置数据源
     *
     * @param list
     */
    protected void setDataResult(List<T> list) {
        if (mlistview == null) {
            return;
        }
        onShowContent();
//        hideDialogLoading();
        if (mCurrentPage == 1) {
            mAdapter.clear();
        }

        if (list != null && list.size() > 0) {
            mAdapter.addAll(list);
            mCurrentPage += 1;
            getEmptyView().setVisibility(View.GONE);

        }  else {
            if (mAdapter.getCount() > 0) {    //表示列表展示的有数据,且加载更多时没有更多
                getEmptyView().setVisibility(View.GONE);
                PrintUtil.toast(getActivity(),   "数据已全部加载");
            }else {
                //mlistview.setEmptyView(getEmptyView());  //表示列表没数据，且请求的数据长度也为0
                getEmptyView().setVisibility(View.VISIBLE);
            }
        }
        if (isLoadMore() || isPullToRefresh()) {
            springView.onFinishFreshAndLoad();
        }

    }


    /**
     * 在更多
     */

    public void onGetMore() {

    }

    /**
     * 刷新
     */
    public void onRefresh() {

    }

    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
    }

    @Override
    public void onRefreshData() {
        onRefresh();
    }

    @Override
    public void onLoadmoreData() {
        onGetMore();
    }

    /**
     * 数据适配器
     *
     * @return
     */
    public abstract BaseListAdapter<T> createAdapter();

    /**
     * 是否加载更多 默认不支持加载更多
     *
     * @return true 是，false 否
     */
    protected boolean isLoadMore() {
        return false;
    }

    /**
     * 是否下拉刷新 默认为不支持
     *
     * @return true 是，false 否
     */
    protected boolean isPullToRefresh() {
        return false;
    }



    protected int getPageSize() {
        return ConstUtil.PAGE_SIZE;
    }

    /**
     * 显示没有数据时背景，
     */
    public void onShowNull(int id) {
        mEmptyImage.setImageResource(id);
    }

    public void setTextNull(String text) {
        mEmptyText.setText(text);
    }
}
